package com.elvin.platform.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.elvin.platform.beans.Device;
import com.elvin.platform.beans.Dictionary;
import com.elvin.platform.beans.UserOrganization;
import com.elvin.platform.mapper.DeviceMapper;
import com.elvin.platform.mapper.DictionaryMapper;
import com.elvin.platform.mapper.OrganizeMapper;
import com.elvin.platform.mapper.UserOrganizationMapper;
import com.elvin.platform.service.DeviceService;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service
@Log4j2
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService {
	@Resource
	private DeviceMapper mapper;
	@Resource
	private OrganizeMapper organizeMapper;
	@Resource
	private UserOrganizationMapper userOrganizationMapper;
	@Resource
  private DictionaryMapper dictionaryMapper;
	@Override
  public void updateReportDate(Device entity) {
    mapper.updateReportDate(entity);
  }
  @Override
  public void updateReportByImei(Device entity) {
    mapper.updateReportByImei(entity);
  }

	@Override
	public List<Device> getDevicesByOrganizeId(Map<String,Object> map) {
		String ids = organizeMapper.selectSelfAndChildId((Long) map.get("organizeId"));
		map.put("ids",ids);
		return mapper.getDevicesByOrganizeId(map);
	}

	@Override
	public int selectPageCount(Device entity) {
		String ids = organizeMapper.selectSelfAndChildId(entity.getOrganizeId());
		entity.setIds(ids);
		return mapper.selectPageCount(entity);
	}

	@Override
	public List<Device> selectForPage(Device entity) {
		String ids = organizeMapper.selectSelfAndChildId(entity.getOrganizeId());
		entity.setIds(ids);
		return mapper.selectForPage(entity);
	}

	@Override
	public List<UserOrganization> getUserOrginzationByDevId(Map<String, Object> map) {
		//1、先根据devId获取设备表中的组织
		//2、根据组织信息获取组织上下级对应的组织
		//3、根据组织查询用户组织表中的所有openid
		List<Device> deviceList = mapper.selectByDevId(map);
		if (deviceList.size()>0){
			log.info("先根据devId获取设备表中的组织,为："+deviceList.get(0).getOrganizeId());
			//获取组织机构信息
			String ids = organizeMapper.selectSelfAndParentId(deviceList.get(0).getOrganizeId());
			log.info("根据组织获取到当前组织和上级组织："+ids);
			map.put("ids",ids);
			List<UserOrganization> list = userOrganizationMapper.selectOpenIdByOrgIds(map);
			log.info("根据组织查询用户组织表中的所有openid,长度为："+list.size());
			return list;
		}else{
			log.info("根据devId未获取到设备信息");
		}
		 return null;
	}

  @Override
  public Integer getOnlineNumber(Device entity) {
    String ids = organizeMapper.selectSelfAndChildId(entity.getOrganizeId());
    entity.setIds(ids);
    return mapper.getOnlineNumber(entity);
  }

    @Override
    public List<Map<String, Object>> getDevTypeNumber(Device entity) {
      String ids = organizeMapper.selectSelfAndChildId(entity.getOrganizeId());
      entity.setIds(ids);
      return mapper.getDevTypeNumber(entity);
    }

    @Override
    public List<Map<String, Object>> getDevCommunicationModeNumber(Device entity) {
      String ids = organizeMapper.selectSelfAndChildId(entity.getOrganizeId());
      entity.setIds(ids);
      return mapper.getDevCommunicationModeNumber(entity);
    }

  @Override
  public List<Device> selectDeviceList(Device entity) {
    String ids = organizeMapper.selectSelfAndChildId(entity.getOrganizeId());
    entity.setIds(ids);
    return mapper.selectDeviceList(entity);
  }

  @Override
  public int selectDevicesBydevScenariosIdCount(Device entity) {
    String ids = dictionaryMapper.selectSelfAndChildId(entity.getDevScenarios());
    entity.setIds(ids);
    return mapper.selectDevicesBydevScenariosIdCount(entity);
  }

  @Override
  public List<Device> selectDevicesBydevScenariosId(Device entity) {
    String ids = dictionaryMapper.selectSelfAndChildId(entity.getDevScenarios());
    entity.setIds(ids);
    return  mapper.selectDevicesBydevScenariosId(entity);
  }
  @Override
  public List<Device> selectByDevType(Device entity) {
    return  mapper.selectByDevType(entity);
  }
  @Override
  public void updateToken(Device entity) {
    mapper.updateToken(entity);
  }
}
